﻿using System;
using System.Web.Mvc;
using ScrumBox.Model;
using MvcContrib;
using MvcContrib.Filters;
using ScrumBox.Web.Helpers;


namespace ScrumBox.Web.Controllers
{
    public class ProductBackLogController : Controller
    {
        private readonly IProjectRepository _projectRepository;
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectRepository"></param>
        public ProductBackLogController(IProjectRepository projectRepository)
        {
            _projectRepository = projectRepository;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectKey"></param>
        /// <returns></returns>
        public ActionResult New(string projectKey)
        {
            var productBackLogItem = new ProductBackLogItem { Project = _projectRepository.GetByKey(projectKey) };
            ViewData.Add(productBackLogItem);
            return View("Edit");
        }

        //TODO: Decide on whether guid's should be used as identifiers, or go back to integers, or title etc???
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="projectKey"></param>
        ///// <param name="backLogItemId"></param>
        ///// <returns></returns>
        //public ActionResult Edit(string projectKey, Guid backLogItemId)
        //{
        //    var project = _projectRepository.GetByKey(projectKey);
        //    var productBackLogItem = project.GetProductBacklogItem(backLogItemId);
        //    ViewData.Add(productBackLogItem);
        //    return View();
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectKey"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public ActionResult Edit(string projectKey, string title)
        {
            var project = _projectRepository.GetByKey(projectKey);
            var productBackLogItem = project.GetProductBacklogItem(title);

            //TODO: Refactor Project assignment, should this be done by NHibernate?
            productBackLogItem.Project = project;
            ViewData.Add(productBackLogItem);
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectKey"></param>
        /// <param name="backLogItemId"></param>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        [PostOnly]
        public ActionResult Save(string projectKey, Guid? backLogItemId, string title, string description)
        {
            ProductBackLogItem productBackLogItem;
            var project = _projectRepository.GetByKey(projectKey) ?? new Project();

            //TODO: Code smell.. refactor.
            if (backLogItemId.HasValue)
            {
                productBackLogItem = project.GetProductBacklogItem(backLogItemId.Value);
            }
            else
            {
                productBackLogItem = new ProductBackLogItem(title, description);
                project.AddProductBackLogItem(productBackLogItem);
            }

            productBackLogItem.Title = title;
            productBackLogItem.Description = description;

            _projectRepository.Save(project);
            return RedirectToRoute("ProjectDetails", projectKey);
        }

    }
}